package GUI;

import export.ExportToExcel;
import service.AdminService;
import service.MerchantService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class AdminFrame extends JFrame implements ActionListener {
    private JFrame frame;
    private JPanel topPanel, centerPanel, warehousePanel, customerPanel, merchantPanel, producerPanel, merchandisePanel, orderPanel, logPanel, helpPanel;
    private JButton warehouseButton, customerButton, merchantButton, producerButton, merchandiseButton, orderButton, logButton, helpButton, exitButton;

    private CardLayout cardLayout;

    private String currentPanel;


    public void AdminFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("管理员界面");

        topPanel = new JPanel(new BorderLayout());
        JPanel buttonPanel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(buttonPanel, BoxLayout.Y_AXIS);
        buttonPanel.setLayout(boxLayout);


        customerButton = new JButton("顾客管理");
        merchantButton = new JButton("商家管理");
        producerButton = new JButton("产商管理");
        merchandiseButton = new JButton("商品管理");
        warehouseButton = new JButton("仓库管理");
        orderButton = new JButton("订单管理");
        logButton = new JButton("日志管理");
        helpButton = new JButton("帮         助");
        exitButton = new JButton("退         出");

        customerButton.addActionListener(this);
        merchantButton.addActionListener(this);
        producerButton.addActionListener(this);
        merchandiseButton.addActionListener(this);
        warehouseButton.addActionListener(this);
        orderButton.addActionListener(this);
        logButton.addActionListener(this);
        helpButton.addActionListener(this);
        exitButton.addActionListener(this);


        buttonPanel.add(customerButton);
        buttonPanel.add(merchantButton);
        buttonPanel.add(producerButton);
        buttonPanel.add(merchandiseButton);
        buttonPanel.add(warehouseButton);
        buttonPanel.add(orderButton);
        buttonPanel.add(logButton);
        buttonPanel.add(helpButton);
        buttonPanel.add(exitButton);

        JPanel contentPanel = new JPanel();

        topPanel.add(buttonPanel, BorderLayout.WEST);
        topPanel.add(contentPanel, BorderLayout.CENTER);

        // 创建中间展示面板和各子面板
        centerPanel = new JPanel();
        cardLayout = new CardLayout();
        centerPanel.setLayout(cardLayout);
        customerPanel = createCustomerPanel();
        merchantPanel = createMerchantPanel();
        producerPanel = createProducerPanel();
        merchandisePanel = createMerchandisePanel();
        warehousePanel = createWarehousePanel();
        orderPanel = createOrderPanel();
        logPanel = createLogPanel();
        helpPanel = createHelpPanel();

        centerPanel.add(customerPanel, "customer");
        centerPanel.add(merchantPanel, "merchant");
        centerPanel.add(producerPanel, "producer");
        centerPanel.add(merchandisePanel, "merchandise");
        centerPanel.add(warehousePanel, "warehouse");
        centerPanel.add(orderPanel, "order");
        centerPanel.add(logPanel, "log");
        centerPanel.add(helpPanel, "help");
        currentPanel = "customer"; // 设置初始面板为顾客管理面板
        frame.getContentPane().add(topPanel, BorderLayout.WEST);
        frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
        frame.setSize(1000, 600);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

        exitButton.addActionListener(e -> {
            frame.dispose();
        });
    }

    // 在actionPerformed方法中添加相应的逻辑
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == warehouseButton) {
            cardLayout.show(centerPanel, "warehouse");
            currentPanel = "warehouse";
        } else if (e.getSource() == customerButton) {
            cardLayout.show(centerPanel, "customer");
            currentPanel = "customer";
        } else if (e.getSource() == merchantButton) {
            cardLayout.show(centerPanel, "merchant");
            currentPanel = "merchant";
        } else if (e.getSource() == producerButton) {
            cardLayout.show(centerPanel, "producer");
            currentPanel = "producer";
        } else if (e.getSource() == merchandiseButton) {
            cardLayout.show(centerPanel, "merchandise");
            currentPanel = "merchandise";
        } else if (e.getSource() == orderButton) {
            cardLayout.show(centerPanel, "order");
            currentPanel = "order";
        } else if (e.getSource() == logButton) {
            cardLayout.show(centerPanel, "log");
            currentPanel = "log";
        } else if (e.getSource() == helpButton) {
            cardLayout.show(centerPanel, "help");
            currentPanel = "help";
        }
    }



    // 创建顾客管理面板
    private JPanel createCustomerPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();
        //创建表格
        JTable table = as.createCustomerJTable();

        //创建添加顾客按钮
        JButton addButton = new JButton("添加顾客");
        addButton.addActionListener(e -> {
            as.addCustomer();
            //刷新表格
            as.refreshCustomerTable(table);
        });

        //创建修改顾客按钮
        JButton modifyButton = new JButton("修改顾客");
        modifyButton.addActionListener(e -> {
            as.updateCustomer(table);
            //刷新表格
            as.refreshCustomerTable(table);
        });

        //创建删除顾客按钮
        JButton deleteButton = new JButton("删除顾客");

        deleteButton.addActionListener(e -> {
            as.deleteCustomer(table);
            //刷新表格
            as.refreshCustomerTable(table);
        });

        // 创建查询文本框和按钮
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(e -> as.searchCustomerTable(table,searchField.getText()));

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> {
            as.refreshCustomerTable(table);
            searchField.setText("");
        });

        // 创建操作面板，并添加按钮和文本框
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(addButton);
        operationPanel.add(modifyButton);
        operationPanel.add(deleteButton);
        operationPanel.add(searchField);
        operationPanel.add(searchButton);
        operationPanel.add(refreshButton);


        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);
        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }


    // 创建商家管理面板
    private JPanel createMerchantPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();
        //创建表格
        JTable table = as.createMerchantJTable();

        //创建添加商家按钮
        JButton addButton = new JButton("添加商家");
        addButton.addActionListener(e -> {
            as.addMerchant();
            //刷新表格
            as.refreshMerchantTable(table);
        });

        //创建修改商家按钮
        JButton modifyButton = new JButton("修改商家");
        modifyButton.addActionListener(e -> {
            as.upadteMerchant(table);
            //刷新表格
            as.refreshMerchantTable(table);
        });

        //创建删除商家按钮
        JButton deleteButton = new JButton("删除商家");
        deleteButton.addActionListener(e -> {
            as.deleteMerchant(table);
            //刷新表格
            as.refreshMerchantTable(table);
        });

        // 创建查询文本框和按钮
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(e -> as.searchMerchantTable(table,searchField.getText()));

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> {
            as.refreshMerchantTable(table);
            searchField.setText("");
        });

        // 创建操作面板，并添加按钮和文本框
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(addButton);
        operationPanel.add(modifyButton);
        operationPanel.add(deleteButton);
        operationPanel.add(searchField);
        operationPanel.add(searchButton);
        operationPanel.add(refreshButton);


        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);
        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    // 创建生产商管理面板
    private JPanel createProducerPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();
        //创建表格
        JTable table = as.createProducerJTable();

        //创建添加生产商按钮
        JButton addButton = new JButton("添加生产商");
        addButton.addActionListener(e -> {
            as.addProducer();
            //刷新表格
            as.refreshProducerTable(table);
        });

        //创建修改生产商按钮
        JButton modifyButton = new JButton("修改生产商");
        modifyButton.addActionListener(e -> {
            as.upadteProducer(table);
            as.refreshProducerTable(table);
        });

        //删除生产商按钮
        JButton deleteButton = new JButton("删除生产商");
        deleteButton.addActionListener(e -> {
            as.deleteProducer(table);
            as.refreshProducerTable(table);
        });

        // 创建查询文本框和按钮
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(e -> as.searchProducerTable(table,searchField.getText()));

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> {
            as.refreshProducerTable(table);
            searchField.setText("");
        });

        // 创建操作面板，并添加按钮和文本框
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(addButton);
        operationPanel.add(modifyButton);
        operationPanel.add(deleteButton);
        operationPanel.add(searchField);
        operationPanel.add(searchButton);
        operationPanel.add(refreshButton);


        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);
        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }



    // 创建商品管理面板
    private JPanel createMerchandisePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();
        //创建表格
        JTable table = as.createShoeJTable();

        //创建添加商品按钮
        JButton addButton = new JButton("添加商品");
        addButton.addActionListener(e -> {
            as.addShoe();
            as.refreshShoeTable(table);
        });

        //创建修改商品按钮
        JButton modifyButton = new JButton("修改商品");
        modifyButton.addActionListener(e -> {
            as.upadteShoe(table);
            as.refreshShoeTable(table);
        });

        //创建删除商品按钮
        JButton deleteButton = new JButton("删除商品");
        deleteButton.addActionListener(e -> {
            as.deleteShoe(table);
            as.refreshShoeTable(table);
        });

        // 创建查询文本框和按钮
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(e -> as.searchShoeTable(table,searchField.getText()));

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> {
            as.refreshShoeTable(table);
            searchField.setText("");
        });

        // 创建操作面板，并添加按钮和文本框
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(addButton);
        operationPanel.add(modifyButton);
        operationPanel.add(deleteButton);
        operationPanel.add(searchField);
        operationPanel.add(searchButton);
        operationPanel.add(refreshButton);

        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);
        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }


    // 创建仓库管理面板
    private JPanel createWarehousePanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();

        // 创建表格
        JTable table = as.createWarehouseJTable();

        //创建添加库存按钮
        JButton addButton = new JButton("添加库存");
        addButton.addActionListener(e -> {
            as.addWarehouseItem();
            as.refreshWarehouseTable(table);
        });

        //创建修改库存按钮
        JButton modifyButton = new JButton("修改库存");
        modifyButton.addActionListener(e -> {
            as.updateWarehouseItem(table);
            as.refreshWarehouseTable(table);
        });

        //创建删除库存按钮
        JButton deleteButton = new JButton("删除库存");
        deleteButton.addActionListener(e -> {
            as.deleteWarehouseItem(table);
            as.updateWarehouseItem(table);
        });

        // 创建查询文本框和按钮
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(e -> as.searchWarehouseTable(table,searchField.getText()));

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> {
            as.refreshWarehouseTable(table);
            searchField.setText("");
        });

        // 创建操作面板，并添加按钮和文本框
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(addButton);
        operationPanel.add(modifyButton);
        operationPanel.add(deleteButton);
        operationPanel.add(searchField);
        operationPanel.add(searchButton);
        operationPanel.add(refreshButton);

        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);
        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }



    // 创建订单管理面板
    private JPanel createOrderPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();
        //创建表格
        JTable table = as.createOrderJTable();

        //点击订单管理自动刷新
        orderButton.addActionListener(e -> {
            as.createOrderJTable();
            as.refreshOrderTable(table);
        });

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> as.refreshOrderTable(table));

        // 创建导出订单按钮
        JButton exportButton = new JButton("导出订单");
        exportButton.addActionListener(e -> {
            try {
                new ExportToExcel().exportAllOrdersToExcel();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        });

        // 创建操作面板，并添加按钮
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(refreshButton);
        operationPanel.add(exportButton);

        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);

        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    // 创建日志管理面板
    private JPanel createLogPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        AdminService as = new AdminService();
        //创建表格
        JTable table = as.createLogJTable();

        //点击订单管理自动刷新
        orderButton.addActionListener(e -> {
            as.createLogJTable();
            as.refreshLogTable(table);
        });

        // 创建刷新按钮
        JButton refreshButton = new JButton("刷新");
        refreshButton.addActionListener(e -> as.refreshLogTable(table));

        // 创建导出订单按钮
        JButton exportButton = new JButton("导出日志");
        exportButton.addActionListener(e -> {
            try {
                new ExportToExcel().exportAllLogsToExcel();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        });

        // 创建操作面板，并添加按钮
        JPanel operationPanel = new JPanel(new FlowLayout());
        operationPanel.add(refreshButton);
        operationPanel.add(exportButton);

        // 创建表格并添加到面板
        JScrollPane scrollPane = new JScrollPane(table);

        // 将操作面板和表格面板添加到主面板
        panel.add(operationPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    // 创建帮助面板
    private JPanel createHelpPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea helpArea = new JTextArea();
        helpArea.setEditable(false);
        helpArea.setText("欢迎使用银行业务自助系统！\n\n"
                + "使用本系统前，请先开通ATM服务。\n"
                + "查询余额：查询当前账户余额。\n"
                + "取款：按照提示输入取款金额。\n"
                + "存款：按照提示输入存款金额，存款金额不受限制。\n"
                + "转账：按照提示输入转账金额和账户号码进行转账操作。\n"
                + "查询日志：查询系统中的交易日志。\n"
                + "退出：退出系统退出。\n"
                + "帮助：查看系统使用帮助。\n");
        panel.add(helpArea, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel(new FlowLayout());
        return panel;
    }

}

